/**
 * @Title: CardServiceImpl.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年5月15日
 */
package com.fortune.rainshinesystem.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.system.CardModel;
import com.fortune.rainshinecommon.util.StringUtil;
import com.fortune.rainshinesystem.mapper.CardMapper;
import com.fortune.rainshinesystem.service.CardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName: CardServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年5月15日
 *
 */
@Service
public class CardServiceImpl implements CardService {
    private static final Logger logger = LoggerFactory.getLogger(CardServiceImpl.class);
    @Autowired
    private CardMapper mapper;

    /**
     * @Title: addCard
     * @Description: 添加会员卡
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年5月15日
     */
    @Override
    public MsgModel addCard(CardModel model) {
        try {
            if(model.getUser()!=null){
                model.setCreateId(model.getUser().getUserId());
                model.setUpdateId(model.getUser().getUserId());
            }
            //会员卡编码
//            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//            model.setCardCode(uuid);
            int res = mapper.insert(model);
            if (res > 0) {
                return new MsgModel("创建会员卡成功！", ResultCode.SUCCESS);
            } else {
                return new MsgModel("创建会员卡失败！", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error("addCard ====> {}", e);
            return new MsgModel("创建会员卡失败！", ResultCode.FAILURE);
        }
    }

    /**
     * @Title: updateCard
     * @Description: 更新会员卡
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年5月15日
     */
    @Override
    public MsgModel updateCard(CardModel model) {
        try {
            if(model.getUser() != null){
                model.setUpdateId(model.getUser().getUserId());
            }
            int res = mapper.updateAllColumnById(model);
            if (res > 0) {
                return new MsgModel("更新会员卡成功！", ResultCode.SUCCESS);
            } else {
                return new MsgModel("更新会员卡失败！", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error("updateCard ====> {}", e);
            return new MsgModel("更新会员卡失败！", ResultCode.FAILURE);
        }
    }

    /**
     * @Title: cardList
     * @Description: 获取会员卡列表
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年5月15日
     */
    @Override
    public MsgModel cardList(ParmsModel parm) {
        try {
            Wrapper<CardModel> wrapper = new EntityWrapper<>();
            if (StringUtil.isNotBlank(parm.getSearchKey())) {
                wrapper.like(parm.getSearchKey(), parm.getSearchValue());
            }
            Page<CardModel> page = new Page<>(parm.getPage(), parm.getLimit());
            List<CardModel> list = mapper.selectPage(page, wrapper.orderBy("create_time", true));
            return new MsgModel(page.getTotal(), list);
        } catch (Exception e) {
            logger.error("cardList ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }

    /**
     * @Title: delCard
     * @Description: 删除会员卡
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年5月15日
     */
    @Override
    public MsgModel delCard(CardModel model) {
        try {
            boolean rs = mapper.deleteById(model.getId()) > 0;
            if (rs) {
                return new MsgModel("删除会员卡成功！", ResultCode.SUCCESS);
            } else {
                return new MsgModel("删除会员卡失败！", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error("delCard ====> {}", e);
            return new MsgModel("删除会员卡失败！", ResultCode.FAILURE);
        }
    }

    /**
     * @Title: getAllCards
     * @Description: 获取所有激活的会员卡
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年5月20日
     */
    @Override
    public List<CardModel> getAllCards() {
        Wrapper<CardModel> wrapper = new EntityWrapper<>();
        wrapper.eq("is_active", 1);
        Page<CardModel> page = new Page<>(0, 999);
        return mapper.selectPage(page, wrapper);
    }
}
